/**
 * Adapter for displaying list of specimens
 */

package edu.washington.cs.labspecimentransport.adapters;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
import edu.washington.cs.labspecimentransport.R;
import edu.washington.cs.labspecimentransport.R.id;
import edu.washington.cs.labspecimentransport.R.layout;
import edu.washington.cs.labspecimentransport.dataobjects.Item;

public class BarcodeAdapter extends BaseAdapter {
	private Context context;
	private List<String> barcodes;
	private List<Integer> ids;
	
	public BarcodeAdapter(Context context, Map<Integer, ArrayList<String>> items) {
		this.context = context;
		this.barcodes = new ArrayList<String>();
		this.ids = new ArrayList<Integer>();
		
		for (Integer i : items.keySet()) {
			ids.add(i);
		}
		
		for (Integer i : this.ids) {
			barcodes.add(items.get(i).get(0));
		}
	}

	public int getCount() {
		return barcodes.size();
	}

	public String getItem(int pos) {
		return barcodes.get(pos);
	}

	public long getItemId(int pos) {
		return barcodes.indexOf(this.getItem(pos));
	}

	public View getView(int position, View view, ViewGroup parent) {
		String barcode = barcodes.get(position);
		LinearLayout itemLayout = (LinearLayout) 
			LayoutInflater.from(context).inflate(R.layout.client_single, parent, false);
		
		// display the barcode of the item
		TextView itemIdField = (TextView) itemLayout.findViewById(R.id.item_id);
		itemIdField.setText(barcode);
		itemIdField.setTag(ids.get(position));
		
		return itemLayout;
	}
}
